package com.rollCall.service.task;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.rollCall.entity.CheckIn;
import com.rollCall.entity.CheckInRecord;
import com.rollCall.entity.No;
import com.rollCall.mapper.CheckInMapper;
import com.rollCall.mapper.CheckInRecordMapper;
import com.rollCall.mapper.NoMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 *
 * @date 2025/2/13
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class DBTask {
	private final NoMapper noMapper;
	private final CheckInMapper checkInMapper;
	private final CheckInRecordMapper checkInRecordMapper;

	@Scheduled(cron = "0 0 0 * * ?")
	public void deleteNo() {
		log.info("删除过期点名编号");
		// 删除数据库中已过期的记录
		noMapper.delete(new QueryWrapper<No>().lt("create_time", LocalDateTime.now().minusDays(1)));
	}

	@Scheduled(cron = "*/10 * * * * ? ")
	public void updateCheckInStatus() {
		log.info("更新过期签到状态");
		// 删除数据库中已过期的记录
		List<CheckIn> expireTime = checkInMapper.selectList(new QueryWrapper<CheckIn>().lt("expire_time", LocalDateTime.now()).orderByAsc("expire_time")
																	.ge("create_time", LocalDateTime.now().minusMinutes(6)));
		expireTime.forEach(e -> {
			QueryWrapper<CheckInRecord> queryWrapper = new QueryWrapper<CheckInRecord>().eq("check_in_id", e.getCheckInId()).eq("status", 0);
			CheckInRecord checkInRecord = new CheckInRecord();
			checkInRecord.setStatus(2);
			checkInRecordMapper.update(checkInRecord, queryWrapper);
		});
	}
}
